import java.util.ArrayList;

/**
 * MiningLaser.java - Plug-in for hey0's minecraft mod.
 * @author Fugiman
 * Special thanks to Sturmeh for the templates
 * I used StarGate by Sturmeh and Dinnerbone as an example for this, go check it out
 */
public class MiningLaser extends ThreadedPlugin {
    private final Listener listener = new Listener();
    private ArrayList<Truck> trucks = new ArrayList<Truck>();
    private int intervalCount = 0;
    
	public MiningLaser() {
		super("MiningLaser", 0.1f);
	}
	
    @Override
    public void initializeExtra() {
        etc.getLoader().addListener(PluginLoader.Hook.PLAYER_MOVE, listener, this, PluginListener.Priority.MEDIUM);
        etc.getLoader().addListener(PluginLoader.Hook.BLOCK_RIGHTCLICKED, listener, this, PluginListener.Priority.MEDIUM);
        etc.getLoader().addListener(PluginLoader.Hook.BLOCK_DESTROYED, listener, this, PluginListener.Priority.MEDIUM);
        etc.getLoader().addListener(PluginLoader.Hook.DISCONNECT, listener, this, PluginListener.Priority.MEDIUM);
        setInterval(5); // 0.25 seconds.
    }
	
    private void onButtonPressed(Player player, Block block) {
    	Truck truck = new Truck(block);
    	trucks.add(truck);
    }
    
    @Override
    public synchronized void doWork() {
    	intervalCount++;
    	for(Truck t : trucks) {
    		t.doWork();
    	}
    }
    
    private class Listener extends PluginListener {
        @Override
        public void onPlayerMove(Player player, Location from, Location to) {
        	
        }

        @Override
        public void onBlockRightClicked(Player player, Block block, Item item) {
        	if ((block.getType() == Truck.BUTTON) && (block.getStatus() == 0)) {
        		onButtonPressed(player,block);
        	}
        }

        @Override
        public boolean onBlockDestroy(Player player, Block block) {
        	if ((block.getType() == Truck.BUTTON) && (block.getStatus() == 0)) {
        		onButtonPressed(player,block);
        		return true;
        	}
        	return false;
        }
    }
}
